package dk.ahp.getmeoff;

import java.io.IOException;
import java.util.List;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;

public class DestinationPart {
	private Location startPoint;
	private Location endPoint;
	
	/**
	 * @param destination Destination for this destination part
	 * @param startPoint Optional starting location.
	 */
	public DestinationPart(Location destination, Location startPoint) {
		if(null != startPoint) {
			this.startPoint = startPoint;
		}
		if(null != destination) { // rather useless, if we do not have destination
			this.endPoint = destination;
		}
	}
	
	public Boolean equals(DestinationPart object) {
		if(this.startPoint != object.startPoint) {
			return false;
		}
		if (this.endPoint != object.endPoint) {
			return false;
		}
		return true;
	}
	
	private String locationToAddres(Location s) {
		String res = new String();
		if (null != s) {
			try {
				Geocoder geocoder = new Geocoder(GetMeOffApplication.getMeOffApplication);
				List<Address> adresser = geocoder.getFromLocation(s.getLatitude(), s.getLongitude(), 1);
				if (adresser != null && adresser.size() > 0) {
					Address a = adresser.get(0);
					res += "\t" + a.getThoroughfare() + " " + a.getFeatureName() + "\n";
					res += "\t" + a.getPostalCode() + " " + a.getLocality();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return res;
	}
	
	public String toString() {
		String res = new String();
		res += "\nStart sted:\n" + locationToAddres(startPoint); 
		res += "\nBestemmelsested sted:\n" + locationToAddres(endPoint);
		return res;
	}
	
	public Location getStartPoint(){
		return startPoint;
	}
	
	public Location getEndPoint() {
		return endPoint;
	}
	
	public void setStartPoint(Location l) {
		startPoint = l;
	}
	
	public void setEndPoint(Location l) {
		endPoint = l;
	}
}
